var Adv_skd=require("./Adv_skd")

var cc__decorate = __decorate;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var $2BundleManager = require("BundleManager");
var $2DataManager = require("DataManager");
var $2PlantMgr = require("PlantMgr");
var $2UIManager = require("UIManager");
var $2MyUtils = require("MyUtils");
var $2Card = require("Card");
var $2UserDataManager = require("UserDataManager");
var $2AudioManager = require("AudioManager");
var $2KDefine = require("KDefine");
var $2NoCoinUI = require("NoCoinUI");
var $2Hts = require("Hts");
var $2TaSdk = require("TaSdk");
var $2ADManager = require("ADManager");
var $2WeChatConstant = require("WeChatConstant");
var $2MenuUI = require("MenuUI");
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_UpgradeUI = /* */function (_cc$Component79) {
  babel_inherits(M, _cc$Component79);
  var _super95 = babel_createSuper(M);
  function M() {
    var _this217;
    babel_classCallCheck(this, M);
    _this217 = _super95.apply(this, arguments);
    _this217.selectPanel = null;
    _this217.rarePanel = null;
    _this217.plantPanel = null;
    _this217.plantInfoPanel = null;
    _this217.goldPanel = null;
    _this217.chipPanel = null;
    _this217.purchasePanel = null;
    _this217.chipIcon1 = null;
    _this217.chipIcon2 = null;
    _this217.chipIcon3 = null;
    _this217.desc = null;
    _this217.videoIcon = null;
    _this217.selectRare = 0;
    _this217.selectID = 0;
    return _this217;
  }
  babel_createClass(M, [{
    key: "onLoad",
    value: function onLoad() {
       Adv_skd.default.instance.hideAllAdv();
                        Adv_skd.default.instance.showInterstitialAd();
                        Adv_skd.default.instance.showGridAdRight();
                        Adv_skd.default.instance.showGridAdLeft();

      $2TaSdk.default.instance.postEvent("System", {
        system: "植物升级"
      });
    }
  }, {
    key: "start",
    value: function start() {
      this.node.on("onClickBuyBtn", this.onClickBuyBtn, this);
      this.node.on("onClickCloseBtn", this.onClickCloseBtn, this);
      this.refreshSelectPanel(null, "0");
      if (!$2UserDataManager.default.instance.upGradeGuide || $2UserDataManager.default.instance.drawGuide && !$2UserDataManager.default.instance.upGradeGuide2) {
        this.onClickPlant(11);
      } else {
        this.onClickPlant(1);
      }
      this.refreshMoneyPanel();
      this.initPlatform();
    }
  }, {
    key: "initPlatform",
    value: function initPlatform() {
      switch ($2ADManager.default.instance.sdkType) {
        case $2ADManager.SDK_TYPE.WeChat:
          $2ADManager.default.instance.showNative(2);
          $2ADManager.default.instance.showNative(3);
          $2ADManager.default.instance.showNative(1);
          $2ADManager.default.instance.hideNative(0);
      }
    }
  }, {
    key: "refreshMoneyPanel",
    value: function refreshMoneyPanel() {
      this.goldPanel.getChildByName("gold").getComponent(cc.Label).string = $2UserDataManager.default.instance.gold;
      if (this.goldEnough()) {
        this.goldPanel.getChildByName("gold").color = cc.color(255, 255, 255);
        this.goldPanel.getChildByName("gold").getComponent(cc.LabelOutline).enabled = true;
      } else {
        this.goldPanel.getChildByName("gold").color = cc.color(255, 0, 0);
        this.goldPanel.getChildByName("gold").getComponent(cc.LabelOutline).enabled = false;
      }
      if (this.chipEnough()) {
        this.chipPanel.getChildByName("chip").color = cc.color(255, 255, 255);
        this.chipPanel.getChildByName("chip").getComponent(cc.LabelOutline).enabled = true;
      } else {
        this.chipPanel.getChildByName("chip").color = cc.color(255, 0, 0);
        this.chipPanel.getChildByName("chip").getComponent(cc.LabelOutline).enabled = false;
      }
    }
  }, {
    key: "chipEnough",
    value: function chipEnough() {
      var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.selectID;
      return $2UserDataManager.default.instance.getPlantChip(e) >= $2PlantMgr.default.instance().getPlant(e).costChip[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
    }
  }, {
    key: "goldEnough",
    value: function goldEnough() {
      var e = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.selectID;
      return $2UserDataManager.default.instance.gold >= $2PlantMgr.default.instance().getPlant(e).costGold[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
    }
  }, {
    key: "onClickBuyBtn",
    value: function onClickBuyBtn() {
      if ($2UserDataManager.default.instance.plantLock.includes(this.selectID)) {
        if (this.goldEnough() && this.chipEnough()) {
          $2UserDataManager.default.instance.gold -= $2PlantMgr.default.instance().getPlant(this.selectID).costGold[$2UserDataManager.default.instance.getPlantLevel(this.selectID) - 1][1];
          $2UserDataManager.default.instance.setPlantChip(this.selectID, -$2PlantMgr.default.instance().getPlant(this.selectID).costChip[$2UserDataManager.default.instance.getPlantLevel(this.selectID) - 1][1]);
          this.upgradePlant();
        } else if (this.goldEnough()) {
          $2UIManager.default.instance.showToast("碎片不足,可前往商店或抽奖获取！");
        } else {
          $2UIManager.default.instance.openUI("NoCoinUI", 50).getComponent($2NoCoinUI.default).init(0);
        }
      } else if ($2UserDataManager.default.instance.getPlantChip(this.selectID) >= $2PlantMgr.default.instance().getPlant(this.selectID).merge) {
        $2UserDataManager.default.instance.getPlant(this.selectID);
        $2UserDataManager.default.instance.setPlantChip(this.selectID, -$2PlantMgr.default.instance().getPlant(this.selectID).merge);
        this.plantPanel.getChildByName("Effect").getComponent(sp.Skeleton).setAnimation(0, "FX", false);
        this.refreshMoneyPanel();
        this.selectPanel.getChildByName(this.selectID + "").getComponent($2Card.default).init(this.selectID);
        this.selectPanel.getChildByName(this.selectID + "").getChildByName("Compound").active = false;
        this.onClickPlant(this.selectID, true);
        $2AudioManager.default.instance.playEffect($2KDefine.AudioName.UpgradeEff);
      } else {
        $2UIManager.default.instance.openUI("ChipTipUI", 99);
      }
    }
  }, {
    key: "upgradePlant",
    value: function upgradePlant() {
      var e = $2UserDataManager.default.instance.getPlantLevel(this.selectID) + 1;
      $2UserDataManager.default.instance.setPlantLevel(this.selectID, e);
      this.refreshMoneyPanel();
      this.refreshUpgradeIcon();
      this.onClickPlant(this.selectID, true);
      this.selectPanel.getChildByName(this.selectID + "").getComponent($2Card.default).init(this.selectID);
      this.plantPanel.getChildByName("Effect").getComponent(sp.Skeleton).setAnimation(0, "FX", false);
      $2AudioManager.default.instance.playEffect($2KDefine.AudioName.UpgradeEff);
      $2Hts.default.instance.eventReport("植物升级成功");
      $2UserDataManager.default.instance.taskInfo[5].progress++;
    }
  }, {
    key: "refreshSelectPanel",
    value: function refreshSelectPanel(e, t) {
      var _this218 = this;
      if (this.selectRare == parseInt(t)) {
        0 != this.selectRare && (this.rarePanel.children[this.rarePanel.childrenCount - this.selectRare].width = 65);
        this.selectRare = 0;
      } else {
        0 != this.selectRare && (this.rarePanel.children[this.rarePanel.childrenCount - this.selectRare].width = 65);
        this.selectRare = parseInt(t);
        this.rarePanel.children[this.rarePanel.childrenCount - this.selectRare].width = 80;
      }
      this.selectPanel.removeAllChildren();
      var a = $2DataManager.default.instance.plantArray;
      var n = $2DataManager.default.instance.newPlant;
      if (n.length >= 1) {
        var _loop7 = function _loop7() {
          var e = cc.instantiate($2BundleManager.default.instance.getBundleRes("Prefab", "card/card_1", cc.Prefab));
          e.getComponent($2Card.default).init(n[_s16]);
          if (e.getComponent($2Card.default).rare == _this218.selectRare || 0 == _this218.selectRare) {
            e.parent = _this218.selectPanel;
            e.getComponent($2Card.default).new.active = true;
            var _t74 = n[_s16];
            e.name = _t74 + "";
            e.on(cc.Node.EventType.TOUCH_END, function () {
              _this218.onClickPlant(_t74);
            });
          } else {
            e.destroy();
          }
        };
        for (var _s16 = 0; _s16 < n.length; _s16++) {
          _loop7();
        }
      }
      var _loop8 = function _loop8(_s17) {
        var e = cc.instantiate($2BundleManager.default.instance.getBundleRes("Prefab", "card/card_1", cc.Prefab));
        e.getComponent($2Card.default).init(a[_s17]);
        if (e.getComponent($2Card.default).rare != _this218.selectRare && 0 != _this218.selectRare || n.includes(a[_s17])) {
          e.destroy();
        } else {
          e.parent = _this218.selectPanel;
          e.name = a[_s17] + "";
          e.on(cc.Node.EventType.TOUCH_END, function () {
            _this218.onClickPlant(a[_s17]);
          });
          !$2UserDataManager.default.instance.plantLock.includes(a[_s17]) && $2UserDataManager.default.instance.getPlantChip(a[_s17]) >= 20 && (e.getChildByName("Compound").active = true);
          if (_this218.chipEnough(a[_s17]) && _this218.goldEnough(a[_s17]) && $2UserDataManager.default.instance.plantLock.includes(a[_s17])) {
            e.getChildByName("UpgradeIcon").active = true;
          } else {
            e.getChildByName("UpgradeIcon").active = false;
          }
        }
      };
      for (var _s17 = 0; _s17 < a.length; _s17++) {
        _loop8(_s17);
      }
    }
  }, {
    key: "refreshUpgradeIcon",
    value: function refreshUpgradeIcon() {
      for (var _e126 = 0; _e126 < this.selectPanel.childrenCount; _e126++) {
        if (this.chipEnough(Number(this.selectPanel.children[_e126].name)) && this.goldEnough(Number(this.selectPanel.children[_e126].name)) && $2UserDataManager.default.instance.plantLock.includes(Number(this.selectPanel.children[_e126].name))) {
          this.selectPanel.children[_e126].getChildByName("UpgradeIcon").active = true;
        } else {
          this.selectPanel.children[_e126].getChildByName("UpgradeIcon").active = false;
        }
      }
    }
  }, {
    key: "onClickPlant",
    value: function onClickPlant(e) {
      var t = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      if (this.selectID == e && !t) {
        return;
      }
      if ($2DataManager.default.instance.newPlant.includes(e)) {
        $2MyUtils.MyUtils.removeArrElement($2DataManager.default.instance.newPlant, e);
        this.selectPanel.getChildByName(e + "").getChildByName("New").active = false;
      }
      this.selectPanel.getChildByName(this.selectID + "") && (this.selectPanel.getChildByName(this.selectID + "").getChildByName("HighLight").active = false);
      if (2 == e || 12 == e) {
        this.plantInfoPanel.getChildByName("Generate").getChildByName("desc").getComponent(cc.Label).string = "生产";
        this.plantInfoPanel.getChildByName("Generate").getChildByName("Icon").getComponent(cc.Sprite).spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "ui/card_info_cost", cc.SpriteFrame);
      } else if (21 == e || 22 == e) {
        this.plantInfoPanel.getChildByName("Generate").getChildByName("desc").getComponent(cc.Label).string = "防御";
        this.plantInfoPanel.getChildByName("Generate").getChildByName("Icon").getComponent(cc.Sprite).spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "ui/card_info_defence", cc.SpriteFrame);
      } else if (23 == e) {
        this.plantInfoPanel.getChildByName("Generate").getChildByName("desc").getComponent(cc.Label).string = "准备时间";
        this.plantInfoPanel.getChildByName("Generate").getChildByName("Icon").getComponent(cc.Sprite).spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "ui/card_info_cd", cc.SpriteFrame);
      } else {
        this.plantInfoPanel.getChildByName("Generate").getChildByName("desc").getComponent(cc.Label).string = "伤害";
        this.plantInfoPanel.getChildByName("Generate").getChildByName("Icon").getComponent(cc.Sprite).spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "ui/card_info_atk", cc.SpriteFrame);
      }
      this.selectPanel.getChildByName(e + "") && (this.selectPanel.getChildByName(e + "").getChildByName("HighLight").active = true);
      var a = $2PlantMgr.default.instance().getPlant(e);
      this.plantPanel.getChildByName("PlantName").getComponent(cc.Label).string = a.name;
      var n = this.plantPanel.getChildByName("Plant").getComponent(sp.Skeleton);
      if ($2BundleManager.default.instance.getBundleRes("Spine", "plant/" + a.dragonName + "/" + a.dragonName, sp.SkeletonData)) {
        n.skeletonData = $2BundleManager.default.instance.getBundleRes("Spine", "plant/" + a.dragonName + "/" + a.dragonName, sp.SkeletonData);
        a.skinName && n.setSkin(a.skinName);
        n.setAnimation(0, "daiji", true);
      } else {
        $2BundleManager.default.instance.loadBundleRes("Spine", "plant/" + a.dragonName + "/" + a.dragonName, sp.SkeletonData, function () {
          n.skeletonData = $2BundleManager.default.instance.getBundleRes("Spine", "plant/" + a.dragonName + "/" + a.dragonName, sp.SkeletonData);
          a.skinName && n.setSkin(a.skinName);
          n.setAnimation(0, "daiji", true);
        });
      }
      this.desc.string = a.desc;
      this.plantPanel.getChildByName("chip").getChildByName("num").getComponent(cc.Label).string = $2UserDataManager.default.instance.getPlantChip(e);
      this.chipIcon1.spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "UpgradeUI/r" + a.rare, cc.SpriteFrame);
      this.chipIcon2.spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "UpgradeUI/r" + a.rare, cc.SpriteFrame);
      this.chipIcon3.spriteFrame = $2BundleManager.default.instance.getBundleRes("BundleTexture", "UpgradeUI/r" + a.rare, cc.SpriteFrame);
      if (10 == $2UserDataManager.default.instance.getPlantLevel(e)) {
        this.plantInfoPanel.getChildByName("Level").getChildByName("num2").getComponent(cc.Label).string = "???";
        this.plantInfoPanel.getChildByName("Generate").getChildByName("num2").getComponent(cc.Label).string = "???";
        this.plantInfoPanel.getChildByName("Cost").getChildByName("num2").getComponent(cc.Label).string = "???";
        this.plantInfoPanel.getChildByName("CD").getChildByName("num2").getComponent(cc.Label).string = "???";
        this.purchasePanel.active = false;
      } else {
        this.plantInfoPanel.getChildByName("Level").getChildByName("num2").getComponent(cc.Label).string = $2UserDataManager.default.instance.getPlantLevel(e) + 1;
        this.plantInfoPanel.getChildByName("Generate").getChildByName("num2").getComponent(cc.Label).string = Math.floor(a.damage[$2UserDataManager.default.instance.getPlantLevel(e)][1] * a.shootNums * a.simpleDamage) + "";
        this.plantInfoPanel.getChildByName("Cost").getChildByName("num2").getComponent(cc.Label).string = a.cost[$2UserDataManager.default.instance.getPlantLevel(e)][1];
        this.plantInfoPanel.getChildByName("CD").getChildByName("num2").getComponent(cc.Label).string = a.cd[$2UserDataManager.default.instance.getPlantLevel(e)][1];
        this.purchasePanel.active = true;
      }
      this.plantInfoPanel.getChildByName("Level").getChildByName("num1").getComponent(cc.Label).string = $2UserDataManager.default.instance.getPlantLevel(e);
      this.plantInfoPanel.getChildByName("Generate").getChildByName("num1").getComponent(cc.Label).string = Math.floor(a.damage[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1] * a.shootNums * a.simpleDamage) + "";
      this.plantInfoPanel.getChildByName("CD").getChildByName("num1").getComponent(cc.Label).string = a.cd[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
      this.plantInfoPanel.getChildByName("Cost").getChildByName("num1").getComponent(cc.Label).string = a.cost[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
      if ($2UserDataManager.default.instance.plantLock.includes(e)) {
        this.purchasePanel.getChildByName("Panel").active = true;
        this.purchasePanel.getChildByName("unLockPanel").active = false;
        this.purchasePanel.getChildByName("buyBtn").getComponentInChildren(cc.Label).string = "升级";
        this.goldPanel.getChildByName("needGold").getComponent(cc.Label).string = "/" + a.costGold[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
        if (0 == a.costChip[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1]) {
          this.chipPanel.active = false;
        } else {
          this.chipPanel.active = true, this.chipPanel.getChildByName("chip").getComponent(cc.Label).string = $2UserDataManager.default.instance.getPlantChip(e) + "", this.chipPanel.getChildByName("needChip").getComponent(cc.Label).string = "/" + a.costChip[$2UserDataManager.default.instance.getPlantLevel(e) - 1][1];
        }
      } else {
        this.purchasePanel.getChildByName("Panel").active = false;
        this.purchasePanel.getChildByName("unLockPanel").active = true;
        this.purchasePanel.getChildByName("unLockPanel").getComponentInChildren(cc.ProgressBar).progress = $2UserDataManager.default.instance.getPlantChip(e) / a.merge;
        this.purchasePanel.getChildByName("unLockPanel").getChildByName("progress").getComponentInChildren(cc.Label).string = $2UserDataManager.default.instance.getPlantChip(e) + "/" + a.merge;
        this.purchasePanel.getChildByName("buyBtn").getComponentInChildren(cc.Label).string = "合成";
      }
      this.selectID = e;
      if (this.chipEnough() && this.goldEnough() && $2UserDataManager.default.instance.plantLock.includes(this.selectID)) {
        this.selectPanel.getChildByName(this.selectID + "").getChildByName("UpgradeIcon").active = true;
      } else {
        this.selectPanel.getChildByName(this.selectID + "").getChildByName("UpgradeIcon").active = false;
      }
      this.refreshMoneyPanel();
    }
  }, {
    key: "onClickCloseBtn",
    value: function onClickCloseBtn() {
      Adv_skd.default.instance.hideAllAdv();

      $2UIManager.default.instance.closeUI("UpgradeUI");
      $2UIManager.default.instance.uiList.MenuUI.getComponent($2MenuUI.default).showNode();
      if ($2ADManager.default.instance.sdkType == $2ADManager.SDK_TYPE.WeChat) {
        $2MyUtils.MyUtils.probability($2WeChatConstant.default.mainInster) && $2UserDataManager.default.instance.levelGuide && $2ADManager.default.instance.showInsert();
        $2ADManager.default.instance.hideNative(2);
        $2ADManager.default.instance.hideNative(3);
        $2ADManager.default.instance.showNative(0);
        $2ADManager.default.instance.hideNative(1);
      }
    }
  }]);
  return M;
}(cc.Component);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "selectPanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "rarePanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "plantPanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "plantInfoPanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "goldPanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "chipPanel", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "purchasePanel", undefined);
cc__decorate([ccp_property(cc.Sprite)], def_UpgradeUI.prototype, "chipIcon1", undefined);
cc__decorate([ccp_property(cc.Sprite)], def_UpgradeUI.prototype, "chipIcon2", undefined);
cc__decorate([ccp_property(cc.Sprite)], def_UpgradeUI.prototype, "chipIcon3", undefined);
cc__decorate([ccp_property(cc.Label)], def_UpgradeUI.prototype, "desc", undefined);
cc__decorate([ccp_property(cc.Node)], def_UpgradeUI.prototype, "videoIcon", undefined);
def_UpgradeUI = cc__decorate([ccp_ccclass], def_UpgradeUI);
exports.default = def_UpgradeUI;